﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using Maticsoft.Common.DEncrypt;
using System.Data;

namespace Maticsoft.Web.Admin.Sys
{
    public partial class edit : PageBase
    {
        string UserId = string.Empty;
        string act = string.Empty;

        BLL.Members bll = new BLL.Members();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("Sys", "modify");

            if(!string.IsNullOrEmpty(Request.QueryString["act"]))
            {
                act = Request.QueryString["act"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                UserId = Request.QueryString["id"];
            }

            if (!IsPostBack)
            {
                if (act == "edit")
                {
                    GetData();
                }
                GetPermission();
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            
            if (act == "add")
            {
                DataSet ds = bll.GetList("UserName='" + txtUserName.Text.Trim() + "'");
                if(ds.Tables[0].Rows.Count>0)
                {
                    MessageBox.ShowAndRedirect(this.Page, "用户名重复", "edit.aspx?act=add");
                    return;
                }
                Model.Members model = new Model.Members();
                model.UserName = txtUserName.Text.Trim();
                model.Password = DEncrypt.Encrypt(txtPassword.Text.Trim());
                model.GroupId = int.Parse(ddlGroup.SelectedValue);
                model.Email = txtEmail.Text.Trim();
                model.Phone = txtPhone.Text.Trim();
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                int userId = bll.Add(model);

                ModifyPermission(userId);

                MessageBox.ShowAndRedirect(this.Page, "添加成功", "list.aspx");
            }
            if (act == "edit")
            {
                DataSet ds = bll.GetList("UserName='" + txtUserName.Text.Trim() + "'");
                if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["UserId"].ToString() != UserId)
                {
                    MessageBox.ShowAndRedirect(this.Page, "用户名重复", "edit.aspx?act=edit&id=" + UserId);
                    return;
                }
                Model.Members model = bll.GetModel(int.Parse(UserId));
                model.UserName = txtUserName.Text.Trim();
                if (txtPassword.Text.Trim() != "")
                {
                    model.Password = DEncrypt.Encrypt(txtPassword.Text.Trim());
                }
                model.GroupId = int.Parse(ddlGroup.SelectedValue);
                model.Email = txtEmail.Text.Trim();
                model.Phone = txtPhone.Text.Trim();
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                bll.Update(model);

                ModifyPermission(model.UserId);

                MessageBox.ShowAndRedirect(this.Page, "修改成功", "list.aspx");
            }
        }
        /// <summary>
        /// 添加修改权限
        /// </summary>
        private void ModifyPermission(int userId)
        {
            BLL.Permission pBll = new BLL.Permission();
            Model.Permission pModel = null;
            pBll.DeleteByUserId(userId);
            string str = txtIds.Value;
            string[] arr = str.Split(',');
            foreach (string s in arr)
            {
                string[] a = s.Split('/');
                pModel = new Model.Permission();
                pModel.UserId = userId;
                pModel.ModuleId = int.Parse(a[0]);
                pModel.ModelName = a[1];
                pModel.Operation = a[2];
                pModel.CreateTime = DateTime.Now;
                pModel.UpdateTime = DateTime.Now;
                pModel.IsActive = 1;
                pBll.Add(pModel);
            }
        }

        private void GetData()
        {
            Model.Members model = bll.GetModel(int.Parse(UserId));
            txtUserName.Text = model.UserName;
            txtEmail.Text = model.Email;
            txtPhone.Text = model.Phone;
            if (model.GroupId == 1)
            {
                ddlGroup.Items.Insert(0, new ListItem("超级管理员", "1"));
                ddlGroup.Enabled = false;
            }
            RequiredFieldValidator4.Enabled = false;
            RequiredFieldValidator2.Enabled = false;
        }

        private void GetPermission()
        {
            BLL.Module mBll = new BLL.Module();
            DataSet ds = mBll.GetAllList();
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected bool hasPermission(string moduleId, string moduleCode, string operation)
        {
            if (act == "edit")
            {
                BLL.Permission pBll = new BLL.Permission();
                DataSet ds = pBll.GetList("UserId=" + UserId + " and moduleId=" + moduleId + " and modelName='" + moduleCode + "' and operation='" + operation + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            return false;
        }
    }
}